package orm;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public abstract class ExerciseDBHelper {

	/**
	 * If Exercise id == 0 new exercise will be created in the db
	 * the row ID of the newly inserted row, or -1 if an error occurred
	 * @param context
	 * @param exercise
	 * @return
	 */
	public static long saveExercise(Context context, Exercise exercise){
		if(exercise.getExercise_id() == 0){
			return addExercise(context, exercise);
		}
		return 111;
		
	}
	
	
	/**
	 * 
	 * @param context
	 * @param person
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	private static long addExercise(Context context, Exercise exercise) {
		
		DBHelper mDbHelper = new DBHelper(context);
		SQLiteDatabase db = mDbHelper.getWritableDatabase();

		long id =  db.insert(DBContract.Exercise.TABLE_NAME,
				DBContract.Exercise.COLUMN_NAME_NULLABLE, getAllValues(exercise));
		mDbHelper.close();
		return id;

	}
	
	/**
	 * 
	 * @param context
	 * @param id
	 * @return null if no FitnessData with the given id is found
	 */
	public static Exercise getExerciseByID(Context context, long id) {
		DBHelper mDbHelper = new DBHelper(context);
		SQLiteDatabase db = mDbHelper.getReadableDatabase();

		String selection = DBContract.Exercise.COLUMN_EXERCISE_ID + " = " + id;

		Cursor c = db.query(DBContract.Exercise.TABLE_NAME, // The table to
																// query
				getAllProjectionCols(), // The columns to return
				selection, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				null // The sort order
				);

		Date date;
		SimpleDateFormat iso8601Format = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		if (c.moveToFirst()) {

			try {
				date = iso8601Format.parse(c.getString(3));
			} catch (java.text.ParseException e) {
				date = null;
				e.printStackTrace();
				mDbHelper.close();
			}
			mDbHelper.close();
			return new Exercise(
					Integer.parseInt(c.getString(0)),
					Integer.parseInt(c.getString(1)), 
					c.getString(2), 
					date
					);
		}
		
		if (c != null && ! c.isClosed()) {
			c.close();
		}
		if (db != null) {
			db.close();
		}
		mDbHelper.close();
		return null;
	}
	
	
	/**
	 * ContentValues: This class is used to store a set of values that the ContentResolver can process.
	 * @param FitnessData
	 * @return a map of all values/columns, where column names are the keys
	 */
	private static ContentValues getAllValues(Exercise exercise){
		
		ContentValues values = new ContentValues();
		values.put(DBContract.Exercise.COLUMN_FITNESSDATA_FK, exercise.getFitnessdata_fk());
		values.put(DBContract.Exercise.COLUMN_EXERCISE_NAME_ENUM, exercise.getExerciseName().toString());
		values.put(DBContract.Exercise.COLUMN_DATE, exercise.getDate().toString());
		
		return values;
	}

	/**
	 * Defines a projection that specifies which columns from the database you
	 * will actually use after this query.
	 * 
	 * @return
	 */
	private static String[] getAllProjectionCols() {

		String[] projection = {
				DBContract.Exercise.COLUMN_EXERCISE_ID,
				DBContract.Exercise.COLUMN_FITNESSDATA_FK,
				DBContract.Exercise.COLUMN_EXERCISE_NAME_ENUM,
				DBContract.Exercise.COLUMN_DATE };

		return projection;
	}
	
	

}
